package com.xbongbong.saas.enums;

import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.business.AssembleEnum;
import com.xbongbong.saas.enums.business.BomBillEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.ContractOutstockEnum;
import com.xbongbong.saas.enums.business.InstockEnum;
import com.xbongbong.saas.enums.business.InventoryEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import com.xbongbong.saas.enums.business.ProductionInstockEnum;
import com.xbongbong.saas.enums.business.ProductionMaterielOutstockEnum;
import com.xbongbong.saas.enums.business.ProductionOrderEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.PurchaseInstockEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.business.RefundInstockEnum;
import com.xbongbong.saas.enums.business.ReturnedMaterielInstockEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseOutstockEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.business.TransferEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import com.xbongbong.saas.enums.business.WorkOrderOutstockEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * 关联产品与关联产品联动关系枚举
 *
 * @author JianTao.Xiong
 * @date 2020/3/31
 */
@Getter
public enum LinkProductRelyEnum {
    // 报价单产品联动机会
    QUOTATION_PRODUCT_RELY_OPPORTUNITY(XbbRefTypeEnum.QUOTATION.getCode(), QuotationEnum.OPPORTUNITY_ID.getAttr(), StringConstant.DATA_ID, SalesOpportunityEnum.PRODUCTS.getAttr(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 合同订单产品联动机会
    CONTRACT_PRODUCT_RELY_OPPORTUNITY(XbbRefTypeEnum.CONTRACT.getCode(), ContractEnum.LINK_OPPORTUNITY.getAttr(), StringConstant.DATA_ID, SalesOpportunityEnum.PRODUCTS.getAttr(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 合同订单产品联动报价单
    CONTRACT_PRODUCT_RELY_QUOTATION(XbbRefTypeEnum.CONTRACT.getCode(), ContractEnum.QUOTATION_ID.getAttr(), StringConstant.DATA_ID, QuotationEnum.PRODUCT.getAttr(), XbbRefTypeEnum.QUOTATION.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 退货退款产品联动合同订单
    REFUND_PRODUCT_RELY_CONTRACT(XbbRefTypeEnum.REFUND.getCode(), RefundEnum.CONTRACT_ID.getAttr(), StringConstant.DATA_ID, ContractEnum.PRODUCT.getAttr(), XbbRefTypeEnum.CONTRACT.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 退货退款关联销售出库单
    REFUND_PRODUCT_RELY_CONTRACT_OUTSTOCK(XbbRefTypeEnum.REFUND.getCode(), RefundEnum.CONTRACT_ID.getAttr(), StringConstant.DATA_ID, ContractOutstockEnum.PRODUCT.getAttr(), XbbRefTypeEnum.CONTRACT_OUTSTOCK.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 销售出库单产品联动合同订单
    CONTRACT_OUTSTOCK_PRODUCT_RELY_CONTRACT(XbbRefTypeEnum.CONTRACT_OUTSTOCK.getCode(), ContractOutstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, ContractEnum.PRODUCT.getAttr(), XbbRefTypeEnum.CONTRACT.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 销售退货入库单产品联动退货退款单
    REFUND_INSTOCK_PRODUCT_RELY_REFUND(XbbRefTypeEnum.REFUND_INSTOCK.getCode(), RefundInstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, RefundEnum.PRODUCTS.getAttr(), XbbRefTypeEnum.REFUND.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 采购入库单产品联动采购合同
    PURCHASE_INSTOCK_PRODUCT_RELY_PURCHASE(XbbRefTypeEnum.PURCHASE_INSTOCK.getCode(), PurchaseInstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, PurchaseEnum.PRODUCT.getAttr(), XbbRefTypeEnum.PURCHASE.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 采购退货单产品联动采购入库
    RETURNED_PURCHASE_PRODUCT_RELY_PURCHASE(XbbRefTypeEnum.RETURNED_PURCHASE.getCode(), ReturnedPurchaseEnum.PURCHASE_SHEET_ID.getAttr(), StringConstant.DATA_ID, PurchaseInstockEnum.PRODUCT.getAttr(), XbbRefTypeEnum.PURCHASE_INSTOCK.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 采购退货出库单产品联动采购退货单
    RETURNED_PURCHASE_OUTSTOCK_PRODUCT_RELY_RETURNED_PURCHASE(XbbRefTypeEnum.RETURNED_PURCHASE_OUTSTOCK.getCode(), ReturnedPurchaseOutstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, ReturnedPurchaseEnum.PRODUCTS.getAttr(), XbbRefTypeEnum.RETURNED_PURCHASE.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 生产单产品联动BOM单
    PRODUCTION_ORDER_FINISH_PRODUCT_RELY_BOM(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), ProductionOrderEnum.BOM_ID.getAttr(), StringConstant.DATA_ID, BomBillEnum.PRODUCT.getAttr(), XbbRefTypeEnum.BOM_BILL.getCode(), SaasMarkEnum.SAAS.getCode()),
    PRODUCTION_ORDER_MATERIEL_PRODUCT_RELY_BOM(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), ProductionOrderEnum.BOM_ID.getAttr(), StringConstant.DATA_ID, BomBillEnum.MATERIEL.getAttr(), XbbRefTypeEnum.BOM_BILL.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 生产入库单产品联动生产单
    PRODUCTION_INSTOCK_PRODUCT_RELY_PRODUCTION(XbbRefTypeEnum.PRODUCTION_INSTOCK.getCode(), ProductionInstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, ProductionOrderEnum.PRODUCT.getAttr(), XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 生产领料单产品联动生产单
    MATERIEL_OUTSTOCK_PRODUCT_RELY_PRODUCTION(XbbRefTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK.getCode(), ProductionMaterielOutstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, ProductionOrderEnum.MATERIEL.getAttr(), XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 生产退料单产品联动生产单
    RETURNED_MATERIEL_INSTOCK_RELY_PRODUCTION(XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK.getCode(), ReturnedMaterielInstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, ProductionMaterielOutstockEnum.PRODUCT.getAttr(), XbbRefTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 其他入库单产品联动装配单
    OTHER_INSTOCK_RELY_ASSEMBLE(XbbRefTypeEnum.OTHER_INSTOCK.getCode(), InstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, AssembleEnum.IN_PRODUCTS.getAttr(), XbbRefTypeEnum.ASSEMBLE.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 其他入库单产品联动调拨单
//    OTHER_INSTOCK_RELY_TRANSFER(XbbRefTypeEnum.OTHER_INSTOCK.getCode(), InstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, TransferEnum.PRODUCTS.getAttr(), XbbRefTypeEnum.TRANSFER.getCode(), SaasMarkEnum.SAAS.getCode()),
    OTHER_INSTOCK_RELY_TRANSFER(XbbRefTypeEnum.OTHER_INSTOCK.getCode(), InstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, OutstockEnum.PRODUCT.getAttr(), XbbRefTypeEnum.OTHER_OUTSTOCK.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 其他入库单产品联动盘点单
    OTHER_INSTOCK_RELY_INVENTORY(XbbRefTypeEnum.OTHER_INSTOCK.getCode(), InstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, InventoryEnum.PRODUCT.getAttr(), XbbRefTypeEnum.INVENTORY.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 其他出库单产品联动装配单
    OTHER_OUTSTOCK_RELY_ASSEMBLE(XbbRefTypeEnum.OTHER_OUTSTOCK.getCode(), OutstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, AssembleEnum.OUT_PRODUCTS.getAttr(), XbbRefTypeEnum.ASSEMBLE.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 其他出库单产品联动调拨单
    OTHER_OUTSTOCK_RELY_TRANSFER(XbbRefTypeEnum.OTHER_OUTSTOCK.getCode(), OutstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, TransferEnum.PRODUCTS.getAttr(), XbbRefTypeEnum.TRANSFER.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 其他出库单产品联动盘点单
    OTHER_OUTSTOCK_RELY_INVENTORY(XbbRefTypeEnum.OTHER_OUTSTOCK.getCode(), OutstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, InventoryEnum.PRODUCT.getAttr(), XbbRefTypeEnum.INVENTORY.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 工单产品联动合同
    WORK_ORDER_PRODUCT_RELY_CONTRACT(XbbRefTypeEnum.WORK_ORDER.getCode(), WorkOrderEnum.LINK_CONTRACT.getAttr(), StringConstant.DATA_ID, ContractEnum.PRODUCT.getAttr(), XbbRefTypeEnum.CONTRACT.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 工单产品联动机会
    WORK_ORDER_PRODUCT_RELY_OPPORTUNITY(XbbRefTypeEnum.WORK_ORDER.getCode(), WorkOrderEnum.LINK_OPPORTUNITY.getAttr(), StringConstant.DATA_ID, SalesOpportunityEnum.PRODUCTS.getAttr(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), SaasMarkEnum.SAAS.getCode()),
    // 工单出库单产品联动工单
    WORK_ORDER_OUTSTOCK_PRODUCT_RELY_WORK_ORDER(XbbRefTypeEnum.WORK_ORDER_OUTSTOCK.getCode(), WorkOrderOutstockEnum.REF_ID.getAttr(), StringConstant.DATA_ID, WorkOrderEnum.SPARE_PARTS.getAttr(), XbbRefTypeEnum.WORK_ORDER.getCode(), SaasMarkEnum.SAAS.getCode());

    /**
     * 当前表单业务类型
     */
    private final Integer buinessType;

    /**
     * 当前联动的字段，如果是子表单数据时，其格式为array.text_1，需要解析掉
     */
    private final String displayField;

    /**
     * 联动显示的字段
     */
    private final String linkField;

    /**
     * 联动字段需要等于的字段
     */
    private final String targetField;
    /**
     * 联动表单业务类型
     *
     * @see com.xbongbong.saas.enums.XbbRefTypeEnum
     */
    private final Integer sourceBusinessType;

    /**
     * saas标识
     *
     * @see com.xbongbong.pro.enums.SaasMarkEnum
     */
    private final Integer sourceSaasMark;

    LinkProductRelyEnum(Integer buinessType, String displayField, String targetField, String linkField, Integer sourceBusinessType, Integer sourceSaasMark) {
        this.buinessType = buinessType;
        this.displayField = displayField;
        this.linkField = linkField;
        this.targetField = targetField;
        this.sourceBusinessType = sourceBusinessType;
        this.sourceSaasMark = sourceSaasMark;
    }

    /**
     * 根据业务类型获取关联关系
     *
     * @param buinessType 业务类型
     * @return 上下游单据关联关系枚举
     */
    public static List<LinkProductRelyEnum> getByBuinessType(Integer buinessType) {
        List<LinkProductRelyEnum> linkProductRelyEnumList = new ArrayList<>();
        for (LinkProductRelyEnum linkProductRelyEnum : LinkProductRelyEnum.values()) {
            if (linkProductRelyEnum.getBuinessType().equals(buinessType)) {
                linkProductRelyEnumList.add(linkProductRelyEnum);
            }
        }
        return linkProductRelyEnumList;
    }

    /**
     * 根据业务类型获取关联关系
     *
     * @param buinessType 业务类型
     * @return 上下游单据关联关系枚举
     */
    public static LinkProductRelyEnum getByBuinessTypeAndSourceBusinessType(Integer buinessType, Integer sourceBusinessType) {
        for (LinkProductRelyEnum linkProductRelyEnum : LinkProductRelyEnum.values()) {
            if (linkProductRelyEnum.getBuinessType().equals(buinessType) && linkProductRelyEnum.getSourceBusinessType().equals(sourceBusinessType)) {
                return linkProductRelyEnum;
            }
        }
        return null;
    }
}
